/*
 * DG2DataContainer.java
 *
 * Created on 13. November 2007
 *
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex.function;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import jsmex.JSmexTools;
import opencard.core.util.HexString;

/**
 *
 * @author Tobias Senger 
 */
public class DG2DataContainer {
    
    private static final byte FACIAL_BIOMETRIC_DATA_GROUP_TAG = (byte) 0x75;
    private static final short BIOMETRIC_INFO_GROUP_TAG = 0x7F61;
    private static final short BIOMETRIC_INFO_TAG = 0x7F60;
    private static final short BIOMETRIC_DATA_TAG = 0x5F2E;
    private static final byte BIOMETRIC_HEADER_TEMPLATE_TAG = (byte) 0xA1;
    private static final byte FORMAT_OWNER_TAG = (byte) 0x87;
    private static final byte FORMAT_TYPE_TAG = (byte) 0x88;
    private BufferedImage image;
    private byte[] imageBytes;
    private byte[] rawData;
    
    /** Creates a new instance of DG2DataContainer */
    public DG2DataContainer(byte[] rawBytes) {
        
        this.rawData = rawBytes.clone();
               
        byte[] bioInfoGroup = ASN1Tools.extractTLV((short)0x7F61,rawBytes,0);
        byte[] bioInfo = ASN1Tools.extractTLV((short)0x7F60,bioInfoGroup,0);
        byte[] bioDataBlock = ASN1Tools.extractTLV((short)0x5F2E,bioInfo,0);
        
        
        //attention! Quick'n'Dirty solution:
        byte[] imageData = new byte[bioDataBlock.length-51];
        System.arraycopy(bioDataBlock,51,imageData,0,imageData.length);
        ByteArrayInputStream dataIn = new ByteArrayInputStream(imageData);
        imageBytes = imageData.clone();
        try {
            image = readImage(dataIn, "image/jpeg2000",413,531);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       
    }
    
    public byte[] getBytes() {
        return rawData;
    }
    
    public byte[] getImageBytes() {
        return imageBytes;
    }
    
    public BufferedImage getImage() {
        return image;
    }
    
    private BufferedImage readImage(InputStream in, String mimeType, int width, int height) throws IOException {
        ImageInputStream iis = ImageIO.createImageInputStream(in);
        Iterator readers = ImageIO.getImageReadersByMIMEType(mimeType);
        while (readers.hasNext()) {
            try {
                ImageReader reader = (ImageReader)readers.next();
                reader.setInput(iis);
                ImageReadParam pm = reader.getDefaultReadParam();
                pm.setSourceRegion(new Rectangle(0, 0, width, height));
                BufferedImage image = reader.read(0, pm);
                if (image != null) {
                    return image;
                }
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        throw new IOException("Could not decode \"" + mimeType + "\" image!");
    }
        
}
